package managedbean;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import model.Client;
import model.Order;
import model.OrderItem;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import dao.OrderDao;

@ManagedBean(name="orderHystoryBean")
@ViewScoped
public class OrderHystoryManagedBean implements Serializable {

	private static final long serialVersionUID = -4621087067846531554L;
	
	@ManagedProperty(value="#{orderDao}")
	private OrderDao orderDao;
	@ManagedProperty(value="#{connexionManagedBean}")
	private ConnexionManagedBean connexionManagedBean;
	
	private List<Order> history;
	private Order selectedOrder;
	
	public OrderDao getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public ConnexionManagedBean getConnexionManagedBean() {
		return connexionManagedBean;
	}

	public void setConnexionManagedBean(ConnexionManagedBean connexionManagedBean) {
		this.connexionManagedBean = connexionManagedBean;
	}

	public List<Order> getHistory() {
		return history;
	}

	public void setHistory(List<Order> history) {
		this.history = history;
	}
	
	public Order getSelectedOrder() {
		return selectedOrder;
	}

	public void setSelectedOrder(Order selectedOrder) {
		this.selectedOrder = selectedOrder;
	}

	@PostConstruct
	public void searchHistoryForUser() {
		Client user = getConnexionManagedBean().getUser();
		setHistory(getOrderDao().getOrders(user));
	}

	public String print (){
		Document pdfGenere =new Document();
		try {
			PdfWriter.getInstance(pdfGenere, new FileOutputStream("D://Facture_"+getSelectedOrder().getId().toString()+".pdf"));
			pdfGenere.open();
			Chunk chunk = new Chunk("Commande pour: "+getSelectedOrder().getClient().getLogin()+" chez Book'tique",
									FontFactory.getFont(FontFactory.COURIER, 20f, Font.ITALIC,BaseColor.BLUE ));
			pdfGenere.addCreationDate();
			pdfGenere.add(chunk);
			chunk = new Chunk("Contenu de la commande:",	FontFactory.getFont(FontFactory.COURIER, 12f, Font.NORMAL,BaseColor.DARK_GRAY ));
			Paragraph block = new Paragraph(chunk);
			pdfGenere.add(block);
			block=new Paragraph();
			pdfGenere.add(block);
			com.itextpdf.text.List liste=new com.itextpdf.text.List();
			for(OrderItem order: getSelectedOrder().getItems()){
				liste.add(new ListItem(order.toString()));
			}
			pdfGenere.add(liste);
			chunk=new Chunk("Total de la comande: "+getSelectedOrder().getTotal());
			pdfGenere.add(chunk);
			pdfGenere.close();
		
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (DocumentException e) {
			
			e.printStackTrace();
		}
		return "printed";
	}
}
